package num152;

/**
 * @author : DuJiabao
 * @Project : sword-finger-offer
 * @Time : 2021/4/14 10:19
 * @Desc :
 */
public class Solution {
  public int maxProduct(int[] nums) {
    int curMax = nums[0]; // 记录当前状态的最大值
    int curMin = nums[0]; // 记录当前状态的最小值
    int res = nums[0]; // 记录最后的结果
    for (int i = 1; i < nums.length; i++) {
      int num = nums[i];  // 避免反复取
      int tempMax = curMax;  // 暂存，因为原来的对象会被修改
      int tempMin = curMin;
      curMax = maxOf(tempMin * num, tempMax * num, num);  // 更新值
      curMin = minOf(tempMin * num, tempMax * num, num);
      res = Math.max(res, curMax);  // 获得结果
    }
    return res;
  }

  private static int maxOf(int a, int b, int c) {
    return Math.max(Math.max(a, b), c);
  }

  private static int minOf(int a, int b, int c) {
    return Math.min(Math.min(a, b), c);
  }

  public static void main(String[] args) {
    //int[] nums = {2, 3, -2, 4};
    //int[] nums = {-2, 0, -1};
    int[] nums = {-4, -3, -2};
    int res = new Solution().maxProduct(nums);
    System.out.println(res);
  }
}
